﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;


/// <summary>
/// 简单宠物AI，三个点之间来回移动
/// </summary>
public class PetAI : MonoBehaviour
{
    public Transform[] targets=new Transform[3];

    int index = 0;


    void Awake()
    {
        targets[0] = GameObject.Find("Pos1").transform;
        targets[1] = GameObject.Find("Pos2").transform;
        targets[2] = GameObject.Find("Pos3").transform;
    }

    void Start()
    {
        StartCoroutine(MoveToTarget());
    }

    IEnumerator MoveTo()
    {
        transform.LookAt(targets[index].position);
        transform.position = Vector3.Lerp(transform.position, targets[index].position, Time.deltaTime * 1);
        if (Vector3.Distance(transform.position, targets[index].position) < 0.1f)
        {
            index++;
            index %= targets.Length;
            yield return new WaitForSeconds(0);
        }
    }

    IEnumerator MoveToTarget()
    {
        while (true)
        {
            yield return StartCoroutine(MoveTo());
        }

    }


}
